<?php

class location_city_model extends CI_Model {

    protected $_instance = array(
        'table' => 'location_city',
        'primaryKey' => 'id',
    );
    public $_data = null;

    public function get_cities($reload = false) {
        $CI =& get_instance();
        $cached_id = 'location/cities';
        if (!GLOBAL_ENABLED_CACHE || $reload) {
            $query = $this->db->get($this->_instance['table']);
            $list_city = array();
            foreach ($query->result() as $row) {
                $list_city[] = $row;
            }
            $CI->st_registry->setKey($cached_id, $list_city, GLOBAL_CACHE_DURATION);
            $query->free_result();
        } else {
            $list_city = $CI->st_registry->getKey($cached_id);
            if (!$list_city) {
                return $this->get_cities(true);
            }
        }
        return $list_city;
    }

    public function add_city($data) {
        $data = $this->auto_render($data);
        $CI = & get_instance();
        if (empty($data['name'])) {
            throw new Exception('Tên thành phố không được để trống');
            return false;
        }
        $data['user_id'] = $CI->session->userdata('admin_id');
        if (empty($data['error'])) {
            $this->db->insert($this->_instance['table'], $data);
        } else {
            throw new Exception($data['error']);
            return false;
        }
        return $this;
    }
    
    public function update_city($id, $data) {
        $data = $this->auto_render($data);
        $CI = & get_instance();
        if (empty($data['name'])) {
            throw new Exception('Tên thành phố không được để trống');
            return false;
        }
        $data['user_id'] = $CI->session->userdata('admin_id');
        if (empty($data['error'])) {
            $this->db->where('id', $id);
            $this->db->update($this->_instance['table'], $data);
        } else {
            throw new Exception($data['error']);
            return false;
        }
        return $this;
    }
    
    public function load_by_id($id) {
        $query = $this->db->get_where($this->_instance['table'], array('id' => $id), 1, 0);
        $this->_data = $query->row();
        $query->free_result();
        return $this->_data;
    }
    
    public function delete_city($id) {
        $this->db->delete($this->_instance['table'], array('id' => $id)); 
        return $this;
    }

    private function auto_render($data) {
        return $data;
    }

    public function get_districts($city_id, $reload = true) {
        $CI =& get_instance();
        $cached_id = 'location/city/id/' . $city_id . '/districts';
        if (!GLOBAL_ENABLED_CACHE || $reload) {
            $list_district = array();
            $query = $this->db->query('SELECT * FROM location_district WHERE city_id = ' . $city_id);
            foreach ($query->result() as $row) {
                $list_district[] = $row;
            }
            $CI->st_registry->setKey($cached_id, $list_district, GLOBAL_CACHE_DURATION);
            $query->free_result();
        } else {
            $list_district = $CI->st_registry->getKey($cached_id);
            if (empty($list_district)) {
                return $this->get_districts($city_id, true);
            }
        }
        return $list_district;
    }
}

?>